package com.sanpu.kxe.business.trade.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.business.card.bean.BankInfoOutput;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.jump.imageloader.JKImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Description：选卡adapter
 * Created by 薛勇军 on 2018/8/9.
 */

public class ChooseBankAdapter extends BaseAdapter {

    private Context mContext;

    private List<BankInfoOutput> infoOutputs = new ArrayList<>();

    private int checkPosition = -1;

    public ChooseBankAdapter(Context mContext, List<BankInfoOutput> infoOutputs) {
        this.mContext = mContext;
        this.infoOutputs = infoOutputs;
    }

    /**
     * 刷新数据
     */
    public void setNotifyData(List<BankInfoOutput> bankList) {
        infoOutputs.clear();
        if (bankList != null) {
            infoOutputs.addAll(bankList);
        }
        notifyDataSetChanged();
    }

    public void setCheckPosition(int checkPosition) {
        this.checkPosition = checkPosition;
    }

    @Override
    public int getCount() {
        if (infoOutputs == null)
            return 0;
        return infoOutputs.size();
    }

    @Override
    public BankInfoOutput getItem(int position) {
        if (infoOutputs == null)
            return null;
        return infoOutputs.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_choose_bank, null);
        }
        TextView vName = convertView.findViewById(R.id.name);
        ImageView vIcon = convertView.findViewById(R.id.icon);
        ImageView vDefIcon = convertView.findViewById(R.id.default_sure);

        BankInfoOutput infoOutput = getItem(position);
        if (infoOutput != null) {
            JKImageLoader.getInstance().displayImage(mContext,
                    UrlConstants.getBaseUrl() + infoOutput.getLogoPath(), vIcon);
            try {
                String bankNo = getItem(position).getBankNo();
                String bankName = getItem(position).getBankName().replace(" ", "");
                vName.setText(bankName + "   (" + bankNo.substring(bankNo.length() - 4, bankNo.length()) + ")");
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (checkPosition == position) {
                vDefIcon.setVisibility(View.VISIBLE);
            } else {
                vDefIcon.setVisibility(View.GONE);
            }
        }
        return convertView;
    }
}
